package com.czk.server;

import com.czk.msg.Request;
import com.czk.msg.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/4/27 11:25
 */
public class MyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("连接到了");
        if (msg instanceof Request) {
            // 强转
            Request request = (Request) msg;
            // 反馈
            Response response = new Response();
            response.setRequestId(request.getRequestId());
            response.setParam(request.getObj() + "请求处理成功");
            // 写回，唤醒客户端
            ctx.writeAndFlush(response);
            // 释放
            ReferenceCountUtil.release(request);
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        // 刷新
        ctx.flush();
    }
}
